package com.nicefact.mailmonitor.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class UserUtil {

	public static String getUserName(){
		UserService service = UserServiceFactory.getUserService();
		String userName = null;
		User user = service.getCurrentUser();
		if(user !=null){
		if(user.getNickname()!=null)
			userName= user.getNickname();
		else if(user.getUserId()!=null)
			userName = user.getUserId();
		else
			userName= user.getEmail();
		}
		return userName;
	}
	public static Boolean isValidEmail(String email){
		return email.matches("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
	}
	public static String getEmail(){
		UserService service = UserServiceFactory.getUserService();
		User user = service.getCurrentUser();
		return user.getEmail();
	}
	
	public static String getDomain(String emailAddr){
		return emailAddr.substring( emailAddr.indexOf("@") + 1 );
	}
	
	public static String getUserNameFromEmail(String emailAddr){
		return emailAddr.substring(0, emailAddr.indexOf("@") );
	}
	
	public static List<Long> getKeysFromString(String deletedCouponQualsKeys) {
		List<Long> keys = new ArrayList<Long>();
		if (!StringUtils.isEmpty(deletedCouponQualsKeys)) {
			if (deletedCouponQualsKeys.indexOf(',') == -1) {
				if (StringUtils.isNumeric(deletedCouponQualsKeys)) {
					keys.add(Long.parseLong(deletedCouponQualsKeys));
				}
			} else {
				List<String> keyStringList = Arrays
						.asList(deletedCouponQualsKeys.split(","));
				for (String key : keyStringList) {
					if (StringUtils.isNumeric(key)) {
						keys.add(Long.parseLong(key));
					}
				}
			}
		}
		return keys;
	}
	
}
